<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>
        <input type="text" id="usename">
    </p>
    <p>
        <input type="text" id="age">
    </p>
    <p>
        <input type="button" value="提交" id='btn'>
    </p>
    <script type="text/javascript">
        //获取按钮元素
        var btn = document.querySelector('#btn');
        //获取姓名文本框
        var usename = document.querySelector('#usename');
        var age = document.querySelector('#age');
        //为按钮添加点击事件
        btn.addEventListener('click', function() {
            //创建ajax对象
            var xhr = new XMLHttpRequest();
            //获取用户在文本框中输入的值
            var namevalue = usename.value;
            var agevalue = age.value;
            //拼接请求参数
            var params = 'usename=' + namevalue + 'age=' + agevalue;
            //配置ajax对象
            xhr.open('get', 'http://localhost:3000/get?' + params);
            //发送请求
            xhr.send();
            //获取服务器端响应的数据
            xhr.onload = function() {
                var responseText = JSON.parse(xhr.responseText);
                console.log(responseText);
            }
        })
    </script>
</body>

</html>